package com.xstablet.util;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.utls.glide.GlideConfig;
import com.xstablet.R;
import com.xstablet.bean.ServiceTeacherListBean;
import com.zyyoona7.popup.EasyPopup;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Administrator on 2020/7/15 0015.
 */

public class TeacherListPopup implements View.OnClickListener {


    private Activity activity;

    TeacherListAdapter listAdapter;
    List<ServiceTeacherListBean.ObjBean.DataBean> teacherList = new ArrayList<>();

    public TeacherListPopup(Activity activity) {

        this.activity = activity;

        initPopuPopu();
    }

    private EasyPopup popu;

    private void initPopuPopu() {
        popu = EasyPopup.create()
                .setContext(activity)
                .setContentView(R.layout.popup_teacherlist)
                .setWidth(WindowManager.LayoutParams.FILL_PARENT)
                .setHeight(WindowManager.LayoutParams.FILL_PARENT)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {

                        initView(view);
                    }

                })
                .setFocusAndOutsideEnable(true)
                .apply();

    }

    private RecyclerView rvList;
    private ImageView ivBefrore, ivNext;
    private TextView tvState;

    private void initView(View view) {

        tvState = view.findViewById(R.id.tv_state);
        rvList = view.findViewById(R.id.rv_list);
        ivBefrore = view.findViewById(R.id.iv_before);
        ivNext = view.findViewById(R.id.iv_next);

        listAdapter = new TeacherListAdapter(activity, teacherList);
        MyLinearLayoutManager manager = new MyLinearLayoutManager(activity, MyLinearLayoutManager.HORIZONTAL, false);
        rvList.setLayoutManager(manager);
        rvList.setAdapter(listAdapter);

        ivBefrore.setOnClickListener(this);
        ivNext.setOnClickListener(this);

    }

    public void setTeacherListData(List<ServiceTeacherListBean.ObjBean.DataBean> teacherListBean) {

        if (teacherListBean != null) {

            if (teacherList.size() > 0) {
                teacherList.clear();
            }
            teacherList.addAll(teacherListBean);
            listAdapter.notifyDataSetChanged();
        }

    }


    public void dismiss() {
        popu.dismiss();
    }


    public void showAnswerPopuPopu(View view) {
        popu.showAtLocation(view, Gravity.CENTER, 0, 0);

    }

    public void setTvState(String state) {
        if (StringUtil.isNotEmpty(state, true)) {
            tvState.setText(state);
        }
    }


    @Override
    public void onClick(View v) {

        int i = v.getId();
        if (i == R.id.iv_before) {

        } else if (i == R.id.iv_next) {

        }
    }

    class TeacherListAdapter extends BaseAdapter<ServiceTeacherListBean.ObjBean.DataBean> {

        public TeacherListAdapter(Context context, List<ServiceTeacherListBean.ObjBean.DataBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.popup_teacherlist_item;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, int position, List<ServiceTeacherListBean.ObjBean.DataBean> mData) {

            ServiceTeacherListBean.ObjBean.DataBean teacherListBean = mData.get(position);

            ImageView ivUser = holder.getView(R.id.iv_user);
            if (StringUtil.isNotEmpty(teacherListBean.getPhoto_url(), true)) {
                Glide.with(activity)
                        .load(teacherListBean.getPhoto_url())
                        .apply(GlideConfig.getCircleOptions())
                        .into(ivUser);
            }

            if (StringUtil.isNotEmpty(teacherListBean.getUser_name(), true)) {
                holder.setText(R.id.tv_name, teacherListBean.getUser_name());
            }

            if (StringUtil.isNotEmpty(teacherListBean.getWork_year() + "", true)) {
                holder.setText(R.id.tv_year, "教龄" + teacherListBean.getWork_year() + "年");
            }

            //老师已服务学生人数以及资格证编号
            if (StringUtil.isNotEmpty(teacherListBean.getTotal_number() + "", true)) {
                holder.setText(R.id.tv_num, teacherListBean.getTotal_number() + "人");
            }

            holder.getView(R.id.lin_item).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (listener != null) {
                        listener.onConnectTeacher(teacherListBean);
                    }

                }
            });

        }
    }

    public interface ConnectTeacherListener {

        void onConnectTeacher(ServiceTeacherListBean.ObjBean.DataBean teacherBean);

    }

    public ConnectTeacherListener listener;

    public void setConnectTeacherListener(ConnectTeacherListener connectTeacherListener) {

        this.listener = connectTeacherListener;
    }

}
